package com.project.base.common;

import java.util.*;

public class LogComparator implements Comparator<String> {
    @Override
    public int compare(String log1, String log2) {
        Date date1 = LogProcessorUtil.getPrintDate(log1);
        Date date2 = LogProcessorUtil.getPrintDate(log2);
        if (date1 == null) {
            return -1;
        }
        if (date2 == null) {
            return 1;
        }
        if (date1.before(date2)) {
            return -1;
        }
        if (date1.after(date2)) {
            return 1;
        }
        return 0;
    }

    public static void main(String[] args) {
        List<String> logList = new ArrayList<String>();
        logList.add("2016-04-13 17:22:32,118 INFO  client-service [FLUME-d3d04cd3-8f38-4dc8-b03f-30dcc31fd298] (ForceUpgradeInterceptor.java:74) - lvversion = null,firstChannel = null-------------------");
        logList.add("2016-04-13 17:22:32,418 INFO  client-service [FLUME-d3d04cd3-8f38-4dc8-b03f-30dcc31fd298] (ForceUpgradeInterceptor.java:74) - lvversion = null,firstChannel = null-------------------");
        logList.add("2016-04-13 17:22:32,018 INFO  client-service [FLUME-d3d04cd3-8f38-4dc8-b03f-30dcc31fd298] (ForceUpgradeInterceptor.java:74) - lvversion = null,firstChannel = null-------------------");
        logList.add("2016-04-13 17:22:32,418 INFO  client-service [FLUME-d3d04cd3-8f38-4dc8-b03f-30dcc31fd298] (ForceUpgradeInterceptor.java:74) - lvversion = null,firstChannel = null-------------------");
        LogComparator com = new LogComparator();
        Collections.sort(logList, com);
        for (String log : logList) {
            System.out.println(log);
        }
    }
}
